package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.mapper.DestinationMapper;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.common.query.DestinationQuery;
import cn.wolfcode.luowowo.common.query.QueryObject;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class DestinationServiceImpl implements IDestinationService {
    @Autowired
    private DestinationMapper destinationMapper;

    @Override
    public PageInfo<Destination> query(DestinationQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Destination> list = destinationMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public List<Destination> toasts(Long parentId) {
        List<Destination> list = new ArrayList<>();
        //利用while循环获取吐司
        Destination dest = destinationMapper.selectByPrimaryKey(parentId);
        while(dest != null){
            dest = destinationMapper.selectByPrimaryKey(dest.getId());
            list.add(dest);
            dest = dest.getParent();
        }
        /*Destination dest = destinationMapper.selectByPrimaryKey(parentId);
        list.add(dest);
        while(dest.getParent() != null ){
            dest = destinationMapper.selectByPrimaryKey(dest.getParent().getId());
            list.add(dest);
        }*/
        //将list倒过来
        Collections.reverse(list);
        return list;
    }

    @Override
    public AjaxResult changeHotState(Long id) {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            Destination dest = destinationMapper.selectByPrimaryKey(id);
            destinationMapper.updateHotState(id, dest.getHot() == 0 ? 1 : 0);
        }catch (Exception e){
            ajaxResult.mark(e.getMessage());
        }
        return ajaxResult;
    }

    @Override
    public List<Destination> selectAllDests() {
        return destinationMapper.selectAllDests();
    }

    @Override
    public List<Destination> selectDestByRegionId(Long rid) {
        return destinationMapper.selectDestByRegionId(rid);
    }

    @Override
    public List<Destination> selectHotDestByRegionId(Long regionId) {
        return destinationMapper.selectHotDestByRegionId(regionId);
    }

    @Override
    public Destination selectByDestId(Long id) {
        return destinationMapper.selectByPrimaryKey(id);
    }

    @Override
    public Destination getCountry(Long destId) {
        List<Destination> toasts = toasts(destId);
        return toasts.size() > 0 ? toasts.get(0) : null;
    }

    @Override
    public Destination getProvince(Long destId) {
        List<Destination> toasts = toasts(destId);
        return toasts.size() > 1 ? toasts.get(1) : null;
    }

    @Override
    public List<Destination> list() {
        return destinationMapper.selectAll();
    }
}
